﻿using FDEEPublicFunc;
using System;
using System.Windows.Forms;

namespace FDEEMain
{
    public partial class ConfigForm : Form
    {
        string fileName = string.Empty; //图框文件名
        string sysConfigPath = CurPaht.curPaht + "\\Config\\BridgeSetting.config";
        bool _isConfig = false;
        string _id = "";

        public ConfigForm(bool isConfig=false,string id="")
        {
            InitializeComponent();
            _isConfig = isConfig;
            _id = id;
            string font = ConfigUtils.GetKey(sysConfigPath, "Font");
            string ft = ConfigUtils.GetKey(sysConfigPath, "TK");
            string sheet = ConfigUtils.GetKey(sysConfigPath, "MapSheet");
            string height = ConfigUtils.GetKey(sysConfigPath, "BottomHeight");
            string width = ConfigUtils.GetKey(sysConfigPath, "LeftWidth");
            try
            {
                if (System.IO.File.Exists(ft))
                {
                    fileName = ft;
                }
                else
                    fileName = "";
            }
            catch
            {
                fileName = null;
            }

            tkPath.Text = fileName;        
            cbSheet.Text = sheet;
            cbFont.Text = font;
            txtBottom.Text = height;
            txtLeft.Text = width;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {           
            if (System.IO.File.Exists(sysConfigPath))
            {
                if (!string.IsNullOrWhiteSpace(cbSheet.Text))
                {
                    Extensions.curMapSheet = cbSheet.Text;
                    ConfigUtils.SetKey(sysConfigPath, "MapSheet", cbSheet.Text);
                }
                if (!string.IsNullOrWhiteSpace(cbFont.Text))
                {
                    Extensions.curFont = cbFont.Text;
                    ConfigUtils.SetKey(sysConfigPath, "Font", cbFont.Text);
                }
                if (!string.IsNullOrWhiteSpace(txtBottom.Text))
                {
                    double height = 30;
                    double.TryParse(txtBottom.Text, out height);
                    Extensions.bottomHeight = height;
                    ConfigUtils.SetKey(sysConfigPath, "BottomHeight", height.ToString());
                }
                if (!string.IsNullOrWhiteSpace(txtLeft.Text))
                {
                    double width = 15;
                    double.TryParse(txtLeft.Text, out width);
                    Extensions.leftWidth = width;
                    ConfigUtils.SetKey(sysConfigPath, "LeftWidth", width.ToString());
                }
                ConfigUtils.SetKey(sysConfigPath, "TK", fileName);
                
                Extensions.curTKPath = fileName;
            }           
            this.DialogResult = DialogResult.OK;
            this.Close();           
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        { 
            //打开文件
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = "dwg";
            dlg.Filter = "dwg Files|*.dwg";
            if (dlg.ShowDialog() == DialogResult.OK)
                fileName = dlg.FileName;
            if (fileName == null)
                return;
            tkPath.Text = fileName;// new System.IO.FileInfo(fileName).Name;
                    
        }

  
    }
}
